package com.youdianstar.app.ui.op

import android.text.Editable
import android.text.TextWatcher
import com.youdianstar.app.common.Extra
import com.youdianstar.app.common.base.BaseActivity
import com.youdianstar.app.common.ex.setThrottleListener
import com.youdianstar.app.common.network.BaseApiResultObserver
import com.youdianstar.app.common.util.MathUtil
import com.youdianstar.app.BR
import com.youdianstar.app.R
import com.youdianstar.app.databinding.OpMerchantWithdrawalBinding
import com.youdianstar.app.entity.CardEntity
import com.youdianstar.app.entity.WithdrawsReq
import com.youdianstar.app.util.DataAppManager
import com.youdianstar.app.common.util.EditUtil
import com.youdianstar.app.common.util.StrUtil
import org.koin.androidx.viewmodel.ext.android.viewModel


/**
 * File: WithdrawalActivity
 * Author: 82149 Create: 2023/8/1 18:42
 * Changes (from 2023/8/1)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
class WithdrawalActivity :BaseActivity<OpMerchantWithdrawalBinding,OPMerchantViewModel>(){
    override fun layoutId(): Int = R.layout.op_merchant_withdrawal

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewModel(): OPMerchantViewModel{
        val model: OPMerchantViewModel by viewModel()
        return model
    }

    private var branchId = 0
    private var balance = 0.0
    private var symbolTx = ""

    override fun initData() {
        super.initData()
        mBinding.headBar.initTitle(this,getString(R.string.shop_withdrawal))
        branchId = intent.getIntExtra(Extra.IT_ID,0)
        balance = intent.getDoubleExtra(Extra.IT_MONEY,0.0)

        symbolTx = DataAppManager.instance.symbolTx()
        mBinding.tvPicCount.text = symbolTx
        mBinding.tvBalance.text = getString(R.string.shop_balance)+": "+ symbolTx+MathUtil.round(balance)

        EditUtil.setEditDecimalFilter(mBinding.edtAmount,2,15)
        mBinding.btnConfirm.isEnabled = false

        mViewModel.getWithdrawalAccount()
    }

    override fun initClickEvent() {
        mBinding.edtAmount.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}

            override fun afterTextChanged(s: Editable?) {
                val inputChar = s.toString()
                if (StrUtil.isEmpty(inputChar)){
                    mBinding.btnConfirm.isEnabled = false
                }else if (StrUtil.isIntFormat(inputChar) || StrUtil.isDoubleFormat(inputChar)){
                    val inputAmount = inputChar.toDouble()
                    mBinding.btnConfirm.isEnabled = inputAmount>0
                }
            }
        })

        mBinding.btnSelectAll.setThrottleListener {

            mBinding.edtAmount.setText(MathUtil.round(balance))
        }

        mBinding.btnConfirm.setThrottleListener {
            val inputTx = mViewModel.amountInputField.get()?:""
            if (StrUtil.isEmpty(inputTx) || inputTx.toDouble()<=0){
                toastShow(getString(R.string.shop_withdrawalAmountText))
                return@setThrottleListener
            }
            if (inputTx.toDouble()>balance){
                toastShow(getString(R.string.shop_withdrawalAmountMax))
                return@setThrottleListener
            }
            mViewModel.amountInputField.get()?.let { amountTx ->
                if (amountTx.toDouble()>0){
                    //调用请求接口
                    mViewModel.requestWithdrawal(WithdrawsReq(branchId,amountTx.toDouble()))
                }
            }
        }
    }


    override fun initLiveObserver() {
        super.initLiveObserver()
        mViewModel.withdrawalAccountLiveData.observe(this,object : BaseApiResultObserver<List<CardEntity>>(canEmpty = true){
            override fun onDataResult(data: List<CardEntity>?) {
                super.onDataResult(data)
                dismissDialog()
                data?.let { cards ->

                }
            }

            override fun onNetError(code: Int, e: String?) {
                super.onNetError(code, e)
                dismissDialog()
                toastShow(e)
            }
        })

        mViewModel.withdrawalResultLiveData.observe(this, object : BaseApiResultObserver<Any>(true) {
            override fun onDataResult(data: Any?) {
                super.onDataResult(data)
                dismissDialog()
                DataAppManager.instance.shopEdtTag = branchId
                finish()
            }

            override fun onNetError(code: Int, e: String?) {
                super.onNetError(code, e)
                dismissDialog()
                toastShow(e)
            }
        })
    }

}